"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/3/14 13:41
@Description : 
  设计模式: 工厂模式   简单工厂模式   解耦合
"""
import json
from abc import ABC, abstractmethod


class Payment(ABC):
    @abstractmethod
    def pay(self):
        pass


class AliPay(Payment):
    def pay(self):
        print('----------支付宝支付-----------')


class WeChatPay(Payment):
    def pay(self):
        print('----------微信支付-----------')


class PaymentFactory:

    @staticmethod
    def get_payment():
        with open('payment.json', 'r', encoding='utf-8') as f:
            data = json.load(f)
            pay_type = data['payType']
        #  通过工厂模式 可以隐藏对象创建的细节 有些对象在创建时需要传递初始化参数
        return WeChatPay() if pay_type == 'wechat' else AliPay()


# 创建支付实例  多态   解耦体现
# payment: Payment = AliPay()
payment: Payment = PaymentFactory.get_payment()
payment.pay()

payment: Payment = PaymentFactory.get_payment()
payment.pay()
